/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.edu.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.edu.entity.EduTeacherState;
import com.jeesite.modules.edu.service.EduTeacherStateService;
import com.jeesite.modules.sys.entity.EmpUser;
import com.jeesite.modules.sys.entity.Employee;
import com.jeesite.modules.sys.entity.Office;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.EmpUserService;
import com.jeesite.modules.sys.service.OfficeService;

import jline.internal.Log;

/**
 * edu_teacher_stateController
 * @author 赵俊飞
 * @version 2019-07-23
 */
@Controller
@RequestMapping(value = "${adminPath}/edu/eduTeacherState")
public class EduTeacherStateController extends BaseController {

	@Autowired
	private EduTeacherStateService eduTeacherStateService;
	@Autowired
	private EmpUserService empUserService;
	@Autowired
	private OfficeService officeService;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public EduTeacherState get(String teacherCode, boolean isNewRecord) {
		return eduTeacherStateService.get(teacherCode, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequestMapping(value = {"list", ""})
	public String list(EduTeacherState eduTeacherState, Model model) {
		model.addAttribute("eduTeacherState", eduTeacherState);
		return "modules/edu/eduTeacherStateList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<EduTeacherState> listData(EduTeacherState eduTeacherState, HttpServletRequest request, HttpServletResponse response) {
		eduTeacherState.setPage(new Page<>(request, response));
		Page<EduTeacherState> page = eduTeacherStateService.findPage(eduTeacherState);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	
	@RequestMapping(value = "form")
	public String form(EduTeacherState eduTeacherState, Model model) {
		model.addAttribute("eduTeacherState", eduTeacherState);
		return "modules/edu/eduTeacherStateForm";
	}

	/**
	 * 保存edu_teacher_state
	 */
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated EduTeacherState eduTeacherState) {
		
		if (!StringUtils.isEmpty(eduTeacherState.getState()) && eduTeacherState.getState().equals("01")) {
			User user = eduTeacherState.getUser();
			EmpUser empUser = empUserService.get(user.getUserCode());
			if(!StringUtils.isEmpty(empUser)) {
				Employee employee = empUser.getEmployee();
				eduTeacherState.getTitle();
				Office office = officeService.getOfficeName(eduTeacherState.getTitle());
				if(StringUtils.isEmpty(office)) {
					return renderResult(Global.FALSE, text("调出失败，学校信息不合法！"));
				}
				empUser.setCreateBy(office.getRemarks());
				employee.setOffice(office);
				employee.setCreateBy(office.getRemarks());
				empUserService.updated(empUser);
				
//				Log.info("既然是调出，就需要再对端系统新增调入数据");
//				EduTeacherState pojo = new EduTeacherState();
//				BeanUtils.copyProperties(eduTeacherState, pojo);
//				pojo.setState("05");
//				pojo.setRemarks(hisOffice.getOfficeCode());
//				eduTeacherStateService.save(pojo);
			}
			
		}
		
		eduTeacherStateService.save(eduTeacherState);
		return renderResult(Global.TRUE, text("保存成功！"));
	}
	
	public static void main(String[] args) {
		EduTeacherState eduTeacherState = new EduTeacherState();
		eduTeacherState.setTitle("111");
		EduTeacherState pojo = new EduTeacherState();
		
		BeanUtils.copyProperties(eduTeacherState, pojo);
		System.out.println(eduTeacherState.getTitle());
		System.out.println(pojo.getTitle());
	}
	
	/**
	 * 删除edu_teacher_state
	 */
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(EduTeacherState eduTeacherState) {
		eduTeacherStateService.delete(eduTeacherState);
		return renderResult(Global.TRUE, text("删除成功！"));
	}
	
}